var app = getApp();
Page({

    data: {
        money: 0,               // 订单金额
        order_no: '',           // 订单号
        isShow: true,
        password: '',           // 密码
        payWay: 1,              // 1 ： 微信支付    2 ： 支付宝支付   3 ： 余额支付

        alipayLink: '',         // 支付宝支付链接
        order_type: "",         // 订单类型 

        is_setPwd:false,        // 是否设置支付密码
    },
    changePayWay(e) {
        var payWay = e.detail.value;
        this.setData({
            payWay: payWay
        });
    },

    // formid
    formSubmit: function(e) {
        var formId = e.detail.formId

        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/user/collect_formid',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        formid: formId
                    },
                    success: function(res) {
                        if (res.data.code == 1) {

                        }
                    }
                })
            }
        })
    },

    // 点击支付按钮触发事件
    payCallBack(e) {
        var type = this.data.payWay;
        var is_setPwd = this.data.is_setPwd;
        if (type == 3) {
            if (!is_setPwd) {
                this.dialog.showDialog();
                return false;
            }
            this.cipher.showPayPassword();
        } else {
            this.paymentSubmit()
        }
    },
    // 支付提交
    paymentSubmit(password) {
        var url = "";
        var order_type = this.data.order_type;

        if (order_type == "entrust_order") {        // 委托支付
            url = "/pays/entrust_bid_pay";
        } else if (order_type == "top_order") {     // 置顶支付
            url = "/pays/top_order";
        } else if (order_type == "charge_order") {  // 充值支付   保证金充值
            url = "/pays/chargePay";
        } else if (order_type == "entrust_buy_pay"){
            url = "/pays/entrust_buy_pay";
        } else {                                    // 普通订单支付
            url = "/pays/index";
        }
        this.pay(url)
    },
    // 支付
    pay(url) {
        wx.showLoading({
            title: '请稍等'
        })
        var payData = {};
        var self = this;
        var password = this.data.password;
        
        payData.trade_no = this.data.order_no;
        payData.pay_type = this.data.payWay;
        if (password) {
            payData.password = password;
        }

        wx.getStorage({
            key: 'user',
            success: function(res) {
                payData.openid = res.data.openid;
                payData.token = res.data.token;
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: payData,
                    success: function(data) {
                        wx.hideLoading();
                        var list = data.data;
                        if (list.code == 1) {
                            if (payData.pay_type == 1) { // 微信支付
                                self.wechatPayment(list.data);
                            } else if (payData.pay_type == 2) { // 支付宝支付
                                self.alipayPayment(list.data)
                            } else if (payData.pay_type == 3) { // 积分钱包支付
                                wx.showToast({
                                    title: list.msg,
                                })
                                wx.redirectTo({
                                    url: '../pay-ok/pay-ok',
                                })
                            }
                        } else {
                            wx.showToast({
                                image: "../../img/warning.png",
                                title: list.msg,
                            })
                        }
                        if (list.code == 20 && payData.pay_type == 3) {
                            self.dialog.showDialog();
                        }
                    },
                })
            },
        })
    },
    // 微信支付
    wechatPayment(data) {
        wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success(res) {
                wx.showToast({
                    title: "支付成功",
                })
                wx.redirectTo({
                    url: '../pay-ok/pay-ok',
                })
            },
            fail(res) {
                wx.showToast({
                    image: '../../img/warning.png',
                    title: '支付失败',
                })
            }
        })
    },
    // 支付宝支付
    // alipayPayment(data){
    //     this.setData({
    //         alipayLink: 'https://www.baidu.com'
    //     })
    // },
    // 生命周期函数--监听页面加载
    onLoad: function(options) {
        // 前往设置支付密码弹窗
        this.dialog = this.selectComponent("#dialog");
        // 获取支付密码输入控件
        this.cipher = this.selectComponent("#password");

        this.setData({
            order_type: options.order_type || "",
            recharge_type: options.type || ""
        })

        if ( options.money && options.order_no ) {
            this.setData({
                money: options.money,
                order_no: options.order_no
            })
        }
        this.is_setPassword();
    },
    // 是否设置支付密码
    is_setPassword(){
        var self = this;
        var url = "/user/is_set_pay_password";

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data:{
                        openid:res.data.openid,
                        token:res.data.token
                    },
                    success: function (req) {
                        var data = req.data;
                        if(data.code == 1){
                            self.setData({is_setPwd: true})
                        }
                        if (data.code == 20) {
                            self.setData({is_setPwd:false})
                        }
                    },

                })
            },
        })
    },
    // 密码输入完成
    _savePassword(e) {
        var password = e.detail.password;

        this.setData({
            password: password
        })
        this.cipher.hidePayPassword();
        // 密码输入完成提交
        this.paymentSubmit(password)
    },

    // 前往设置支付密码 - 确认
    __YesEvent() {
        this.dialog.hideDialog();
        wx.navigateTo({
            url: '../my-tx-apply-form/my-tx-apply-form',
        })
    },
    // 前往设置支付密码 - 取消
    __NoEvent() {
        this.dialog.hideDialog();
    },
});